package main

import (
	"encoding/base64"
	"encoding/hex"
	"fmt"

	"gitee.com/gogm/gmsm/sm2"
)

func PrintCert(data string) {
	fmt.Println("-----BEGIN CERTIFICATE-----")
	nums := len(data) / 64
	mod := len(data) % 64
	i := 0
	for i = 0; i < nums-1; i++ {
		fmt.Println(data[i*64 : (i+1)*64])
	}
	if mod > 0 {
		fmt.Println(data[(i+1)*64:])
	}
	fmt.Println("-----END CERTIFICATE-----")

}

func DecodeCerts() {

	certs01 := "3082021a308201c1a00302010202090093eced1db7b5296d300a06082a811ccf55018375308182310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c534f5242206f6620544153533116301406035504030c0d546573742043412028534d3229301e170d3230303632333033313034365a170d3234303830313033313034365a308186310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c42535243206f662054415353311a301806035504030c11736572766572207369676e2028534d32293059301306072a8648ce3d020106082a811ccf5501822d034200043afd5333584a347fde39ab18c51d3b137020024b5db125228d86748b257fbb73ffea061f0d5fd4ad28e26d292b50c0d52d93c1db733141441a009cd10ad09a7ba31a301830090603551d1304023000300b0603551d0f0404030206c0300a06082a811ccf550183750347003044021f49b61b3d68462ba2d77c210dc34133ba6d858d2ee843ae5690b50f43d1d285022100f5d1b83500f8b16946e5577f98891e73b0ac270fe8eeda850090644cd430fc05"
	certs02 := "3082025930820200a003020102020900ef22e36e3251c4e9300a06082a811ccf55018375308182310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c534f5242206f6620544153533116301406035504030c0d546573742043412028534d3229301e170d3230303632333033313034365a170d3234303830313033313034365a308182310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c534f5242206f6620544153533116301406035504030c0d546573742043412028534d32293059301306072a8648ce3d020106082a811ccf5501822d03420004f1dbb0f540da8cbab8010ed828af66288df6ae814b087f9730156b67cd9d9082fe002f7672e3bbd11cff166247f6893899dfa3d9f6399d7d27191380ea81b289a35d305b301d0603551d0e041604140eea3c16b949c8d8969947462ff1ddcc2e25f8ac301f0603551d230418301680140eea3c16b949c8d8969947462ff1ddcc2e25f8ac300c0603551d13040530030101ff300b0603551d0f040403020106300a06082a811ccf55018375034700304402200cbfb0b8c3934c18d904b4bf69cf21a17c5d1ce1f16bf7e49521982db31ca072022074d3782fd33d5bd78739c031ec2d1f068b2e8116ccc1c832d1439549bf7c1340"
	certs03 := "30820219308201c0a00302010202090093eced1db7b5296e300a06082a811ccf55018375308182310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c534f5242206f6620544153533116301406035504030c0d546573742043412028534d3229301e170d3230303632333033313034365a170d3234303830313033313034365a308185310b300906035504061302434e310b300906035504080c02424a3110300e06035504070c074861694469616e31253023060355040a0c1c4265696a696e67204a4e544120546563686e6f6c6f6779204c54442e31153013060355040b0c0c42535243206f6620544153533119301706035504030c1073657276657220656e632028534d32293059301306072a8648ce3d020106082a811ccf5501822d03420004d712ffb5d887aef68fa30a808ec24c9ba575267844cefea2847a222a51013168a3ef609d870e67358a820733e28e27fdfa3ae207e8c1988976497d9433836c50a31a301830090603551d1304023000300b0603551d0f040403020338300a06082a811ccf55018375034700304402207395a8969b1634918a011cf031de67302b6bd6c79290b529609ab685dd8205080220328c1ff8262c746f461cbdf2c2b2ff108c9eba9070d7138d924c8dd8d15f0a47"

	cd1, _ := hex.DecodeString(certs01)
	cd2, _ := hex.DecodeString(certs02)
	cd3, _ := hex.DecodeString(certs03)

	cert1, err := sm2.ParseCertificate(cd1)
	if err != nil {
		fmt.Println("err->", err)
		return
	}

	fmt.Println("\n\nCert1-->", cert1.SerialNumber)
	PrintCert(base64.StdEncoding.EncodeToString(cd1))
	cert2, err := sm2.ParseCertificate(cd2)
	if err != nil {
		fmt.Println("err->", err)
		return
	}
	fmt.Println("\n\nCert2-->", cert2.SerialNumber)
	PrintCert(base64.StdEncoding.EncodeToString(cd2))
	cert3, err := sm2.ParseCertificate(cd3)
	if err != nil {
		fmt.Println("err->", err)
		return
	}
	fmt.Println("\n\nCert3-->", cert3.SerialNumber)
	PrintCert(base64.StdEncoding.EncodeToString(cd3))
}

func main() {
	DecodeCerts()
}
